﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using ProjControleModelos;

namespace ProjControleDAL
{
   public class MarcasDAL
    {
       Dados objD = null;
      SqlCommand cmd = null;

      public MarcasDAL()
      {
      }
        #region Métodos

      //inserir
      public void inserir(Marcas objM)
      {
          try
          {
              cmd = new SqlCommand();
              objD = new Dados();
              cmd.CommandText = "insert into marcas values(@descricao)";
              cmd.Parameters.Add(new SqlParameter("@descricao", SqlDbType.VarChar)).Value = objM.descricao_marca;
              objM.cod_marca = objD.executaComandoAI(cmd, "grupos");
          }
          catch (Exception ex)
          {
              
              throw ex;
          }
          cmd = null;
          objD = null;
          
      }
      //alterar
      public void alterar(Marcas objM)
      {
          try
          {
              cmd = new SqlCommand();
              objD = new Dados();
              cmd.CommandText = "update marcas set descricao_marca = @descricao where cod_marca = @codigo";
              cmd.Parameters.Add(new SqlParameter("@descricao", SqlDbType.VarChar)).Value = objM.descricao_marca;
              cmd.Parameters.Add(new SqlParameter("@codigo", SqlDbType.Int)).Value = objM.cod_marca;
              objD.executaComando(cmd);
          }
          catch (Exception ex)
          {
              
              throw ex;
          }
          cmd = null;
          objD = null;
      }
      public void excluir(Marcas objM)
      {
          try
          {
              cmd = new SqlCommand();
              objD = new Dados();
              cmd.CommandText = "delete from marcas where cod_marca = @codigo";
              cmd.Parameters.Add(new SqlParameter("@codigo", SqlDbType.Int)).Value = objM.cod_marca;
              objD.executaComando(cmd);
          }
          catch (Exception ex)
          {
              
              throw ex;
          }
          cmd = null;
          objD = null;
      }
      public Marcas localizar(int codigo)
      {
          Marcas objM = null;
          DataTable tab;
          try
          {
              cmd = new SqlCommand();
              objD = new Dados();
              cmd.CommandText = "select * from marcas where cod_marca = @codigo";
              cmd.Parameters.Add(new SqlParameter("@codigo", SqlDbType.Int)).Value = codigo;
              tab = objD.executaConsulta(cmd);
              if(tab != null)
                  if (tab.Rows.Count == 1)
                  {
                      objM = new Marcas(codigo, tab.Rows[0]["descricao_marca"].ToString());
                  }
              
          }
          catch (Exception ex)
          {
              
              throw ex;
          }
          cmd = null;
          objD = null;
          return objM;
      }
      public DataTable localizar()
      {
          
          DataTable tab;
          try
          {
              objD = new Dados();
              cmd = new SqlCommand();
              cmd.CommandText = "select * from marcas order by descricao_marca";
              tab = objD.executaConsulta(cmd);
          }
          catch (Exception ex)
          {
              
              throw ex;
          }
          cmd = null;
          objD = null;
          return tab;
      }
        #endregion
    }
}
